//
//  NSBundle+LLPackages.m
//  live.ly
//
//  Created by bob on 16/5/12.
//  Copyright © 2016年 musical.ly. All rights reserved.
//

#import "NSBundle+LLPackages.h"
//#import "LLPackagesAppService.h"


@implementation NSBundle (LLPackages)

//+ (instancetype)ll_bundleWithPackageName:(NSString *)packageName  andMainRes(NSString*)mainRootPath
+ (instancetype)ll_bundleWithPackageName:(NSString *)packageName  andStockRootPath:(NSString*)mainRootPath
{
    //NSString *downloadedPath = [[LLPackagesAppService sharedInstance] packagePath:packageName];
    
    NSString *packageRootPath = [mainRootPath stringByAppendingPathComponent:packageName];

    
    NSString *downloadedPath;
    NSBundle *downloadBundle;
    if (downloadedPath) {
        downloadBundle = [NSBundle bundleWithPath:downloadedPath];
    }
#ifdef UseLocalBundleOnly
    downloadBundle = nil;
#endif
    NSBundle *localGiftBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"face_stock_packages" ofType:@"bundle"]];
    NSString *path = [localGiftBundle pathForResource:packageName ofType:@"bundle"];
    NSBundle *localBundle = [NSBundle bundleWithPath:path];
    if (downloadBundle.infoDictionary && localBundle.infoDictionary) {
        NSString *versionLocal = [localBundle objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
        NSString *versionRemote = [downloadBundle objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
        if ([versionRemote compare:versionLocal options:NSNumericSearch] == NSOrderedDescending) {
            return downloadBundle;
        }
        return localBundle;
    }
    return downloadBundle ?: localBundle;
}

@end
